package models

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

/**
 *  连接数据库
 */
func init() {
	//连接数据库
	// set default database
	//orm.RegisterDataBase("default", "mysql", "root:123123123@tcp(127.0.0.1:3306)/blog?charset=utf8", 30)
	//orm.RegisterDataBase("default", "mysql", "root:root@tcp(127.0.0.1:3306)/blog?charset=utf8", 30)
	dbhost := beego.AppConfig.String("db_host")
	dbport := beego.AppConfig.String("db_port")
	dbuser := beego.AppConfig.String("db_user")
	dbpassword := beego.AppConfig.String("db_password")
	dbname := beego.AppConfig.String("db_name")
	if dbport == "" {
		dbport = "3306"
	}
	dsn := dbuser + ":" + dbpassword + "@tcp(" + dbhost + ":" + dbport + ")/" + dbname + "?charset=utf8&loc=Asia%2FShanghai"
	orm.RegisterDataBase("default", "mysql", dsn, 30)
	// register model，RegisterModel 也可以同时注册多个 model，orm.RegisterModel(new(User), new(Profile), new(Post))
	orm.RegisterModel(new(NoteBook), new(UserTable), new(Super), new(MessagePad), new(Thumbup), new(Critic), new(Notice), new(Link))
	// create table
	orm.RunSyncdb("default", false, true)
}

//xss过滤
// 正则过滤sql注入的方法
